package com.doctor.ui.homedoctor;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.doctor.bean.PatientFileListBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.FragmentHelper;
import com.doctor.comm.LoadingTip;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragmentActivity;
import com.doctor.ui.R;
import com.doctor.ui.consulting.view.ConsultingActivity;
import com.doctor.ui.medicalknowledge.KnowledgeActivity;
import com.doctor.ui.office.OfficeActivity;

public class HomeDoctorActivity extends BaseFragmentActivity{

	/**
	 * 家庭医生健康管理
	 */
	
	private PatientFileFrag homeDoctorFrag;
	private SearchPatientFrag searchPatientFrag;
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
        setContentView(R.layout.base_activity);
		
		initiaView();
		initiaSubMenu();
		getData();
		
		//通知用户升级新数据
		noticeNewData();
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//改变主菜单背景颜色
		officeBtn.setBackgroundResource(R.color.light_green);
		knowledgeBtn.setBackgroundResource(R.color.light_green);
		consultingBtn.setBackgroundResource(R.color.light_green);
		homeDocBtn.setBackgroundResource(R.color.green);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//改变主菜单背景颜色
		homeDocBtn.setBackgroundResource(R.color.light_green);
	}
	
	@Override
	protected void initiaSubMenu() {
		// TODO Auto-generated method stub
		super.initiaSubMenu();
		subMenu.setVisibility(View.GONE);
		subMenu.setEnabled(false);
		
		FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
		homeDoctorFrag=null;
		homeDoctorFrag=new PatientFileFrag();
		FragmentHelper.setFragSwitchAnimation(transaction);
		transaction.replace(fragContainer.getId(), homeDoctorFrag).commit();
	}
	
	
	
	@Override
	protected void onHeaderClicked() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onOfficeClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(HomeDoctorActivity.this,OfficeActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void onMedicalKnowledgeClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(HomeDoctorActivity.this,KnowledgeActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void onConsultingClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(HomeDoctorActivity.this,ConsultingActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void searchByKeyWord(final String keyWord) {
		// TODO Auto-generated method stub
		super.searchByKeyWord(keyWord);
		
		LoadingTip.showDialog(HomeDoctorActivity.this, "正在搜索......");
		new Thread() {
			public void run() {
				final PatientFileListBean list=DbOperator.getInstance(HomeDoctorActivity.this).searchPatient(keyWord);
				runOnUiThread(new Runnable() {
					public void run() {
						if (list.getSize()<=0) {
							DialogHelper.showConfirmDialog(HomeDoctorActivity.this, "没有相关搜索结果，请更换搜索关键词再试试！");
						} else {
							searchPatientFrag=null;
							searchPatientFrag=new SearchPatientFrag();
							Bundle bundle=new Bundle();
							bundle.putSerializable(ConstConfig.BEAN, list);
							FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
							searchPatientFrag.setArguments(bundle);
							FragmentHelper.setFragSwitchAnimation(transaction);
							transaction.replace(fragContainer.getId(), searchPatientFrag).addToBackStack(null).commit();	
						}
						LoadingTip.hideDialog();
					}
				});
			}
		}.start();
	}
	
}
